package org.dromara.autocode.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.dromara.autocode.constant.UserConstants;
import org.dromara.autocode.domain.entity.SysAutoCodePart;
import org.dromara.autocode.mapper.SysAutoCodePartMapper;
import org.dromara.autocode.service.SysAutoCodePartService;
import org.dromara.autocode.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.web.core.BaseService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 编码生成规则组成Service业务层处理
 *
 * @author wenrui Su
 * @date 2025-03-19
 */
@RequiredArgsConstructor
@Service
public class SysAutoCodePartServiceImpl extends BaseService implements SysAutoCodePartService {

    private final SysAutoCodePartMapper baseMapper;

    /**
     * 查询编码生成规则组成
     */
    public SysAutoCodePart queryById(Long partId){
        return baseMapper.selectById(partId);
    }

    /**
     * 查询编码生成规则组成列表
     */
    public Page<SysAutoCodePart> queryPageList(SysAutoCodePart bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SysAutoCodePart> lqw = buildQueryWrapper(bo);
        Page<SysAutoCodePart> result = baseMapper.selectPage(pageQuery.build(), lqw);
        return result;
    }

    /**
     * 查询编码生成规则组成列表
     */
    public List<SysAutoCodePart> queryList(SysAutoCodePart bo) {
        LambdaQueryWrapper<SysAutoCodePart> lqw = buildQueryWrapper(bo);
        return baseMapper.selectList(lqw);
    }

    private LambdaQueryWrapper<SysAutoCodePart> buildQueryWrapper(SysAutoCodePart bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysAutoCodePart> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getRuleId() != null, SysAutoCodePart::getRuleId, bo.getRuleId());
        lqw.eq(bo.getPartIndex() != null, SysAutoCodePart::getPartIndex, bo.getPartIndex());
        lqw.eq(StringUtils.isNotBlank(bo.getPartType()), SysAutoCodePart::getPartType, bo.getPartType());
        lqw.eq(StringUtils.isNotBlank(bo.getPartCode()), SysAutoCodePart::getPartCode, bo.getPartCode());
        lqw.like(StringUtils.isNotBlank(bo.getPartName()), SysAutoCodePart::getPartName, bo.getPartName());
        lqw.eq(bo.getPartLength() != null, SysAutoCodePart::getPartLength, bo.getPartLength());
        lqw.eq(StringUtils.isNotBlank(bo.getDateFormat()), SysAutoCodePart::getDateFormat, bo.getDateFormat());
        lqw.eq(StringUtils.isNotBlank(bo.getInputCharacter()), SysAutoCodePart::getInputCharacter, bo.getInputCharacter());
        lqw.eq(StringUtils.isNotBlank(bo.getFixCharacter()), SysAutoCodePart::getFixCharacter, bo.getFixCharacter());
        lqw.eq(bo.getSeriaStartNo() != null, SysAutoCodePart::getSeriaStartNo, bo.getSeriaStartNo());
        lqw.eq(bo.getSeriaStep() != null, SysAutoCodePart::getSeriaStep, bo.getSeriaStep());
        lqw.eq(bo.getSeriaNowNo() != null, SysAutoCodePart::getSeriaNowNo, bo.getSeriaNowNo());
        lqw.eq(StringUtils.isNotBlank(bo.getCycleFlag()), SysAutoCodePart::getCycleFlag, bo.getCycleFlag());
        lqw.eq(StringUtils.isNotBlank(bo.getCycleMethod()), SysAutoCodePart::getCycleMethod, bo.getCycleMethod());
        lqw.eq(StringUtils.isNotBlank(bo.getAttr1()), SysAutoCodePart::getAttr1, bo.getAttr1());
        lqw.eq(StringUtils.isNotBlank(bo.getAttr2()), SysAutoCodePart::getAttr2, bo.getAttr2());
        lqw.eq(bo.getAttr3() != null, SysAutoCodePart::getAttr3, bo.getAttr3());
        lqw.eq(bo.getAttr4() != null, SysAutoCodePart::getAttr4, bo.getAttr4());
        lqw.eq(bo.getCreateDept() != null, SysAutoCodePart::getCreateDept, bo.getCreateDept());
        return lqw;
    }

    /**
     * 新增编码生成规则组成
     */
    public Boolean insertByBo(SysAutoCodePart bo) {
        validEntityBeforeSave(bo);
        boolean flag = baseMapper.insert(bo) > 0;
        return flag;
    }

    /**
     * 修改编码生成规则组成
     */
    public Boolean updateByBo(SysAutoCodePart bo) {
        validEntityBeforeSave(bo);
        return baseMapper.updateById(bo) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SysAutoCodePart entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除编码生成规则组成
     */
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

  @Override
  public List<SysAutoCodePart> listPart(SysAutoCodePart sysAutoCodePart) {
    return baseMapper.selectSysAutoCodePartList(sysAutoCodePart);
  }

  @Override
  public SysAutoCodePart findById(Long partId) {
    return baseMapper.findById(partId);
  }

  @Override
  public String checkPartUnique(SysAutoCodePart sysAutoCodePart) {
    SysAutoCodePart part = baseMapper.checkPartUnique(sysAutoCodePart);
    Long partId = StringUtils.isNull(sysAutoCodePart.getPartId())?-1L:sysAutoCodePart.getPartId();
    if(StringUtils.isNotNull(part) && partId.longValue() != part.getPartId().longValue()){
      return UserConstants.NOT_UNIQUE;
    }
    return UserConstants.UNIQUE;
  }

  @Override
  public int insertPart(SysAutoCodePart sysAutoCodePart) {
    return baseMapper.add(sysAutoCodePart);
  }

  @Override
  public int updatePart(SysAutoCodePart sysAutoCodePart) {
    return baseMapper.updateSysAutoCodePart(sysAutoCodePart);
  }

  @Override
  public int deleteByIds(Long[] partIds) {
    for (Long partId:partIds
    ) {
      baseMapper.deleteById(partId);
    }
    return 1;
  }


}
